有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如果在使用JDBC时不包含db驱动程序会发生什么?

我是java开发新手,很高兴看到与我习惯的php环境相比,在支持多个平台时,数据库实现要容易得多

然而,有一件事让我感到困惑——我在所有我必须做的事情中都读到了我想要使用的数据库驱动程序的运行时包含,即:

班级。forName(com.example.jdbc.Driver)。newInstance()

然而,忽略这个似乎也很好——所以我的问题是,给定服务器url,jdbc是否自动找到驱动程序?如果是的话,为什么这一行包含在我阅读的所有关于这个主题的教程中

另外,如果有人对在线java学习资源(特别是企业开发)有什么好的建议,请分享


共 (2) 个答案

  1. # 1 楼答案

    是的,这在JDK 6中得到了改进。见this O'Reilly article for JDBC 4.0 improvements

    特别是:

    In JDBC 4.0, we no longer need to explicitly load JDBC drivers using Class.forName(). When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from among the JDBC drivers that were loaded at initialization and those loaded explicitly using the same class loader as the current application.

    另请参见JDK 6中的JavaDoc for DriverManager

  2. # 2 楼答案

    JDK 6中没有发生这种变化吗